package bist.believeinstep.impl;

import javax.sql.DataSource;

import org.springframework.jdbc.core.JdbcTemplate;

import bist.believeinstep.dao.ArticleImageDAO;
import bist.believeinstep.dao.ConnectionDAO;
import bist.believeinstep.domain.ArticleImage;

public class ArticleImageTemplate implements ConnectionDAO, ArticleImageDAO{

	private JdbcTemplate jdbcTemplate;
	
	@Override
	public void setDataSource(DataSource dataSource) {
		this.jdbcTemplate = new JdbcTemplate(dataSource);
	}
	
	@Override
	public boolean create(ArticleImage articleImage) {
		String query = "INSERT INTO bist_article_images(bist_articles_id, bist_images_id, created_date, modified_date, created_by, modified_by)" +
				" VALUES(?,?)";
		try {
			jdbcTemplate.update(query, new Object[]{
				articleImage.getArticleId(),
				articleImage.getImageId(),
				articleImage.getCreated(),
				articleImage.getModified(),
				articleImage.getCreatedBy(),
				articleImage.getModifiedBy()
			});
			return true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}

	@Override
	public boolean update(ArticleImage articleImage) {
		String query = "UPDATE bist_article_images " +
				"SET bist_articles_id = ?" +
				" bist_images_id = ?" +
				" modified_date = ?" +
				" modified_by = ?" +
				" WHERE id = ?";
		try {
			jdbcTemplate.update(query, new Object[]{
				articleImage.getArticleId(),
				articleImage.getImageId(),
				articleImage.getModified(),
				articleImage.getModifiedBy(),
				articleImage.getId()
			});
			return true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}

	@Override
	public boolean delete(int id) {
		String query = "DELETE bist_article_images WHERE id = ?";
		try {
			jdbcTemplate.update(query, new Object[]{id});
			return true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}
}
